/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

/**
 *
 * @author bob
 */
public class Payments extends javax.swing.JFrame {

    /**
     * Creates new form Payments
     */
    PopUpTable pt;
    
    
    public Payments() throws SQLException {
        initComponents();        
        textUpdater();
        
    }

    private void textUpdater()
    {
        SwingWorker text = new SwingWorker()
        {
            ResultSet rset;
            boolean running = true;
            @Override
            protected Object doInBackground() throws Exception 
            {
                String[] strArray;
                
             //   while(running)
                {
                    
                    System.out.println("WAT");
                     
                     strArray = pt.stringFromRow();
                     System.out.println(strArray[4]);
                     tenantBox.setText(strArray[4]);
                    
                }
              return new Object();
              
                   //     throw new UnsupportedOperationException("Not supported yet.");
            }
             @Override
             protected void done() {
                System.out.println("DONE");
             }
        };
        
        text.execute();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        searchButton = new javax.swing.JButton();
        searchBox = new javax.swing.JTextField();
        tenantBox = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        amountBox = new javax.swing.JTextField();
        submitButton = new javax.swing.JButton();
        backButton = new javax.swing.JButton();
        paymentBox = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                formFocusGained(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Payments", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14), java.awt.Color.black)); // NOI18N
        jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jPanel1MouseEntered(evt);
            }
        });
        jPanel1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jPanel1FocusGained(evt);
            }
        });

        jLabel1.setText("Payment Type:");

        jLabel2.setText("Tenant:");

        searchButton.setText("Search");
        searchButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                searchButtonMouseClicked(evt);
            }
        });

        searchBox.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                searchBoxMouseClicked(evt);
            }
        });

        tenantBox.setEditable(false);
        tenantBox.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tenantBoxMouseClicked(evt);
            }
        });

        jLabel3.setText("Amount:");

        amountBox.setEditable(false);
        amountBox.setText("1000.00");
        amountBox.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                amountBoxMouseClicked(evt);
            }
        });

        submitButton.setText("Submit");
        submitButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                submitButtonMouseClicked(evt);
            }
        });

        backButton.setText("Back");
        backButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                backButtonMouseClicked(evt);
            }
        });

        paymentBox.setEditable(false);

        jLabel4.setText("Apt Number:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(searchButton))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(submitButton)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(searchBox, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(paymentBox, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(amountBox, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                        .addComponent(tenantBox, javax.swing.GroupLayout.Alignment.LEADING)))
                .addContainerGap(116, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(backButton))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchButton)
                    .addComponent(searchBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tenantBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(paymentBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(amountBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(submitButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addComponent(backButton))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void backButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_backButtonMouseClicked
        //create main window
        MainMenu main = new MainMenu();
        main.setLocation(MainMenu.X, MainMenu.Y);
        main.setVisible(true);
                
        //close this window
        this.dispose(); 
    }//GEN-LAST:event_backButtonMouseClicked

    private void update() throws SQLException
    {
        String[] text = pt.stringFromRow();
        String query;
            if(text.length >1 )
            {                
                query = "UPDATE BILL SET PAYDATE = NOW() WHERE TSSN = ? AND BILLDATE = ?";
                DBconnection db = new DBconnection();
                Connection conn = db.getConnection();
                PreparedStatement statement = null;
                statement = conn.prepareStatement(query);                             
                ///////////////////////////////
                System.out.println(text[0]);
                text[0] = text[0].replaceAll("-", "/");
               
                System.out.println(text[3]);
                
                statement.setString(1, text[3]);                
                statement.setString(2, text[0]);
               
                ///////////////////////////////
                statement.executeUpdate();
                statement.close();
                
                System.out.println("SUCCESS");

                
            }// TODO add your handling code here:
    }
    private void submitButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_submitButtonMouseClicked
        //if boxes are not empty then submit, otherwise print error message
        if(!tenantBox.getText().isEmpty() && !amountBox.getText().isEmpty() && !paymentBox.getText().isEmpty())
        {  
            /*
            int choice = JOptionPane.showMessageDialog(this, "Payment submitted. "
                    + "\nWould you like to print a receipt?"
                    ,"", JOptionPane.OK_OPTION,
                    JOptionPane.INFORMATION_MESSAGE
                    );
                    * */
            String st="Payment properly submitted";
            JOptionPane.showMessageDialog(rootPane, st, null, JOptionPane.INFORMATION_MESSAGE); 
            try {
                    update();
                } catch (SQLException ex) {
                    Logger.getLogger(Payments.class.getName()).log(Level.SEVERE, null, ex);
                }
            //if(choice == 0)
            {

            }
        
        }
        else
        {
            String st="Please search an apartment number";
            JOptionPane.showMessageDialog(rootPane, st, null, JOptionPane.ERROR_MESSAGE); 
        }
    }//GEN-LAST:event_submitButtonMouseClicked

    private void searchButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_searchButtonMouseClicked
        if(!searchBox.getText().isEmpty()) 
        {
            pt = new PopUpTable("SELECT * FROM BILL WHERE PAYDATE IS NULL AND TSSN IN (SELECT SSN FROM TENANT WHERE APTNO = " + Integer.parseInt(searchBox.getText())+ " )");
            pt.setVisible(true);
            editing = false;
        }
        else
        {
            String st="Please enter an apartment number";
            JOptionPane.showMessageDialog(rootPane, st, null, JOptionPane.ERROR_MESSAGE); 
        }
         
         //String str = pt.stringFromRow();
    }//GEN-LAST:event_searchButtonMouseClicked

    private void formFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusGained
        // TODO add your handling code here:
    }//GEN-LAST:event_formFocusGained

    private void jPanel1FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jPanel1FocusGained

       
    }//GEN-LAST:event_jPanel1FocusGained
boolean editing = true;
    private void jPanel1MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel1MouseEntered
        if(!editing)
        {
            String[] text = pt.stringFromRow();
            if(text.length >1 )
            {
                System.out.println(text.length);        
                tenantBox.setText(text[3]);
                amountBox.setText(text[2]);
                paymentBox.setText(text[1]);             
                
                editing = true;
            }// TODO add your handling code here:
        }
    }//GEN-LAST:event_jPanel1MouseEntered

    private void searchBoxMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_searchBoxMouseClicked
        editing = true;       
        searchBox.selectAll();
    }//GEN-LAST:event_searchBoxMouseClicked

    private void tenantBoxMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tenantBoxMouseClicked
        editing = true;
        //tenantBox.selectAll();
    }//GEN-LAST:event_tenantBoxMouseClicked

    private void amountBoxMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_amountBoxMouseClicked
        editing = true;
        amountBox.selectAll();
    }//GEN-LAST:event_amountBoxMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField amountBox;
    private javax.swing.JButton backButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField paymentBox;
    private javax.swing.JTextField searchBox;
    private javax.swing.JButton searchButton;
    private javax.swing.JButton submitButton;
    private javax.swing.JTextField tenantBox;
    // End of variables declaration//GEN-END:variables
}
